package com.nbsaas.boot.controller.web;

import com.nbsaas.boot.handle.TalkHandler;
import com.nbsaas.boot.rest.response.ListResponse;
import com.nbsaas.boot.rest.response.MapResponse;
import io.netty.channel.Channel;
import io.netty.channel.group.ChannelGroup;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/state")
public class StateController {


    @ResponseBody
    @RequestMapping("/list")
    public ListResponse<MapResponse> list() {
        ChannelGroup group = TalkHandler.users;
        ListResponse<MapResponse> result=new ListResponse<>();
        result.setData(new ArrayList<>());

        for (Channel channel : group) {
            MapResponse item = new MapResponse();
            item.put("id",channel.id().asShortText());
            item.put("ip",channel.remoteAddress()+"");

            result.getData().add(item);
        }

        return result;
    }
}
